package com.huazi.www.doctor.hu.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.huazi.www.doctor.hu.entity.*;
import com.huazi.www.doctor.hu.page.Page;
import com.huazi.www.doctor.hu.service.DoctorService;
import com.huazi.www.doctor.hu.service.impl.DoctorServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipFile;

public class DoctorServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");//转格式
        resp.setContentType("text/html;charset=utf-8");//转格式
        //创建DoctorServiceImpl对象，对象名为service
        DoctorServiceImpl service = new DoctorServiceImpl();
        String czf = req.getParameter("czf");//获取操作符
        //如果操作符为空，默认为查询病人
        if (czf == null || czf.equals("")) {
            czf = "selPt";
        }
        String currentPage = req.getParameter("currentPage");//获取当前页currentPage
        //如果没有当前页,默认为1
        if (currentPage == null || currentPage.equals("")) {
            currentPage = "1";
        }

        //返回药单的集合，调用service的list方法
        List<Drug> list = service.list();
        //将list集合存入request
        req.setAttribute("list", list);


        //检查单开具，当czf等于ctqueue时
        if (czf.equals("ctqueue")) {
            //获取id，转换成int类型
            int id = Integer.parseInt(req.getParameter("id"));
            //调用service的checkinsert方法，参数为id
            boolean boo = service.checkinsert(id);
            //用Ajax进行输出
            PrintWriter pw = resp.getWriter();
            //将boolean转换为JSON类型
            String bool = JSON.toJSONString(boo);
            pw.print(bool);
            pw.flush();
            pw.close();

        }


        //开药,当czf等于kaiyao
        if (czf.equals("kaiyao")) {
            //诊断号 id，转换成int
            int id = Integer.parseInt(req.getParameter("id"));
            //患者名字 name
            String name = req.getParameter("name");
            //医生id doctorid
            int doctorid = Integer.parseInt(req.getParameter("doctorid"));
            //时间 time
            String time = req.getParameter("time");
            //获得病例 bingli
            String bingli = req.getParameter("bingli");
            //获得药品id drugid 数量 drugsum 医嘱 drugtype
            String strid = req.getParameter("drugid");
            String strsum = req.getParameter("drugsum");
            String strtype = req.getParameter("drugtype");
            //去掉药品id drugid 数量 drugsum 医嘱 drugtype中的-，分割成数组
            String[] drugid = strid.split("-");
            String[] drugsum = strsum.split("-");
            String[] drugtype = strtype.split("-");
            //创建List<Medicine>集合，保存Medicine对象
            List<Medicine> insertlist = new ArrayList<>();
            for (int i = 0; i < drugid.length; i++) {
                //创建Medicine对象
                Medicine m = new Medicine();
                m.setUserid(id);
                m.setUsername(name);
                m.setMedicineid(Integer.parseInt(drugid[i]));
                m.setSum(Integer.parseInt(drugsum[i]));
                m.setTime(time);
                m.setDoctorid(doctorid);
                m.setType(drugtype[i]);
                //将Medicine对象加入list集合
                insertlist.add(m);
            }
            //调用service的insertlist方法，参数为insertlist,bingli
            service.insertlist(insertlist, bingli);


        }
        //当czf等于selPt时，查询病人
        if (czf.equals("selPt")) {
            //获取医生id,保存在session的doctorId
            String doctorId = (String) req.getSession().getAttribute("doctorId");
            //通过调用service的selPat方法，参数为currentPage，doctorId，创建Page<PatientInfo>分页对象
            Page<PatientInfo> pat = service.selPat(Integer.parseInt(currentPage), Integer.parseInt(doctorId));//获取病人分页对象
            req.setAttribute("pat", pat);//保存病人分页对象
            req.getRequestDispatcher("doctor/prescribe.jsp").forward(req, resp);//转发到doctor/prescribe.jsp页面
        } else if (czf.equals("selCt")) {
            //显示CT结果
            //通过ctNo获取病人编号
            String no = req.getParameter("ctNo");
            //调用service的selCtCount方法，参数为no(转Integer)，判断是否有CT记录
            boolean selCt = service.selCtCount(Integer.parseInt(no));
            //用Ajax输出结果
            PrintWriter out = resp.getWriter();
            //如果有ct记录
            if (selCt) {
                //调用service的selCt方法，参数为no(转Integer)，显示ct记录
                List<CtInfo> ct = service.selCt(Integer.parseInt(no));
                //创建JSONArray对象保存ct记录的集合
                JSONArray array = new JSONArray();
                array.addAll(ct);
                out.print(array);
                out.flush();
                out.close();
            } else {
                //没有记录输出null
                out.print("null");
                out.flush();
                out.close();
            }


//            String s = JSON.toJSONString(selCt);
//            out.print(s);
//            out.flush();
        } else if (czf.equals("selDis")) {
            //显示以往病例
            //通过ctNo获取病人编号
            String no = req.getParameter("ctNo");
            //通过调用service的selDiCount方法，参数是no(转Integer)，判断是否有CT记录
            boolean selDis = service.selDiCount(Integer.parseInt(no));
            //用Ajax输出
            PrintWriter out = resp.getWriter();
            //如果有历史病例
            if (selDis) {
                //通过调用service的selDisease方法,参数为no,创建List<DiseaseInfo>对象
                List<DiseaseInfo> disease = service.selDisease(Integer.parseInt(no));
                //创建JSONArray对象保存历史病例的集合
                JSONArray array = new JSONArray();
                array.addAll(disease);
                out.print(array);
                out.flush();
                out.close();
            } else {
                //如果没有历史病例，输出null
                out.print("null");
                out.flush();
                out.close();
            }
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
